package com.itany.shop.controller;

import com.itany.shop.entity.Item;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.RequestParameterException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.OrderService;
import com.itany.shop.service.ProductService;
import com.itany.shop.vo.Cart;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.util.List;
import java.util.Scanner;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年12月21日 17:12
 */
public class MainController {
    private User user;
    //当用户成功登陆后，给用户一个购物车
    private Cart cart = new Cart();
    private Scanner sc = new Scanner(System.in);

    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartService");
    private OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");

    public MainController(User user) {
        this.user = user;
    }

    // 请选择服务(1.加入购物车 2.查看购物车)：1
    // 商品编号   商品名称  商品单价
    //     1       XX       1000
    // ...
    //     13     XX      2000
    // 请选择需要添加入到购物车的商品编号:1
    // 添加成功
    // 请选择服务(1.加入购物车 2.查看购物车):2
    // 商品编号   商品名称   数量    总价
    //    1        XX         2      2000

    public void showMenu() {
        System.out.println("-------------欢迎登陆网络商城-----------");
        System.out.print("请选择服务(1.加入购物车 2.查看购物车)");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
        }
    }

    public void addCart() {
        List<Product> producs = productService.findAll();

        System.out.println("--------------------------");
        System.out.println("商品编号\t\t商品名称\t\t商品价格");
        for (Product product : producs) {
            System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
        }
        System.out.print("请输入需要添加到购物车中的商品编号");
        Integer id = sc.nextInt();
        try {
            cartService.addCart(cart, id);
            System.out.println("添加购物车成功");
        } catch (UserNotExistException e) {
            e.printStackTrace();
        } catch (RequestParameterException e) {
            e.printStackTrace();
        } catch (ProductNotExistException e) {
            e.printStackTrace();
        }

        showMenu();
    }

    public void showCart() {
        System.out.println("商品编号\t\t商品名称\t\t商品数量\t\t总价");
        for (Item item : cart.getItems()) {
            System.out.println(item.getProduct().getId() + "\t\t" + item.getProduct().getName() + "\t\t" + item.getNum() + "\t\t" + item.getPrice());
        }
        System.out.println("----------------------");
        System.out.println("购物车总价：" + cart.getPrice());
        System.out.println("----------------------");
        System.out.println("请选择(1.删除商品 2.修改商品数量 3.清空购物车 4.结算)");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                removeCart();
                break;
            case 2:
                modifyCart();
                break;
            case 3:
                clearCart();
                break;
            case 4:
                createOrder();
                break;
        }

    }

    public void removeCart() {
        System.out.println("请输需要删除的商品编号:");
        Integer id = sc.nextInt();
        try {
            cartService.removeCart(cart, id);
        } catch (UserNotExistException e) {
            System.out.println(e.getMessage());
        } catch (RequestParameterException e) {
            System.out.println(e.getMessage());
        }
        showMenu();
    }

    public void modifyCart(){
        System.out.print("请输入需要修改的商品编号:");
        int id=sc.nextInt();
        System.out.print("请输入修改后的数量:");
        int num=sc.nextInt();
        try {
            cartService.modifyCart(cart,id,num);
            System.out.println("修改成功");
        } catch (UserNotExistException e) {
            System.out.println(e.getMessage());
        } catch (RequestParameterException e) {
            e.getMessage();
        }
        showMenu();
    }
    public void clearCart(){
        cart=new Cart();
        showMenu();
    }
    public void createOrder(){
        try {
            String no=orderService.createOrder(cart,user);
            //结算成功，清空当前的购物车
            cart=new Cart();
            System.out.println("结算成功，您的订单号为:"+no);
        } catch (UserNotLoginException e) {
            System.out.println("结算失败"+e.getMessage());
            e.printStackTrace();
        }
        showMenu();
    }

}
